package com.jftone.bh.app.action;

import java.util.List;

import com.jftone.bh.app.config.SysConst;
import com.jftone.bh.app.data.StaticInfoQry;
import com.jftone.bh.app.model.SysParam;
import com.jftone.bh.app.service.SysParamService;
import org.apache.log4j.Logger;
import org.jftone.annotation.Autowired;
import org.jftone.annotation.Controller;
import org.jftone.exception.ActionException;
import org.jftone.exception.ComponentException;
import org.jftone.util.DataMap;
import org.jftone.util.IData;
import org.jftone.util.StringUtil;


@Controller(mapping="/sysParamAction")
public class SysParamAction extends BaseAction {
	
	private Logger logger = Logger.getLogger(SysParamAction.class);
	
	@Autowired
	private SysParamService service;
	
	/**
	 * 加载参数列表
	 * @throws ActionException
	 */
	public void query() throws ActionException{
		IData<String, Object> data = getData();
		String dataType = data.getString("dataType", SysConst.ROOT_CODE);
		try {
			IData<String, Object> inData = new DataMap<String, Object>();
			inData.put("dataType", SysConst.ROOT_CODE);
			List<SysParam> rootList = service.querySysParam(inData);
			List<SysParam> paramList = null;
			if(!dataType.equals(SysConst.ROOT_CODE)){
				inData.clear();
				inData.put("dataType", dataType);
				paramList  = service.querySysParam(inData);
			}else{
				paramList = rootList;
			}
			//设置功能按钮
			setMenuFuncDerictive();
			//设置隐藏传值参数
			this.putRenderInfo(SysConst.QUERY_PARAMS, putParam("dataType", dataType).getParam());
			
			this.putRenderInfo("dataType", dataType);
			this.putRenderInfo("params", paramList);
			this.putRenderInfo("root", rootList);
		} catch (Exception e) {
			logger.error("查询参数数据错误"+e.getMessage(), e);
			throw new ActionException("查询参数数据错误"+e.getMessage(), e);
		}
		this.render("sys_param.html");
	}
	
	/**
	 * 新增参数类别
	 * @throws ActionException
	 */
	public void add() throws ActionException{
		IData<String, Object> data = getData();
		String dataType = data.getString("dataType", SysConst.ROOT_CODE);
		SysParam param = new SysParam();
		try {
			String dataTypeName = "系统参数类别";
			if(!StringUtil.isBlank(dataType) && !dataType.equals(SysConst.ROOT_CODE)){
				IData<String, Object> inData = new DataMap<String, Object>();
				inData.put("dataCode", dataType);
				inData.put("dataType", SysConst.ROOT_CODE);
				SysParam sysParam  = service.getSysParam(inData);
				dataTypeName = sysParam.getDataName();
			}
			param.setDataType(dataType);
			this.putRenderInfo("dataTypeName", dataTypeName);
			this.putRenderInfo("param", param);
		} catch (Exception e) {
			throw new ActionException("查询系统参数数据错误"+e.getMessage(), e);
		}
		this.render("sys_param_form.html");
	}
	
	public void edit() throws ActionException{
		IData<String, Object> data = getData();
		String dataType = data.getString("dataType", SysConst.ROOT_CODE);
		String dataId = data.getString("dataId", "");
		if(StringUtil.isBlank(dataId) || !StringUtil.isNumber(dataId)){
			this.alertMsg("参数传值错误");
			return;
		}
		try {
			String dataTypeName = "系统参数类别";
			IData<String, Object> inData = new DataMap<String, Object>();
			inData.put("id", Integer.parseInt(dataId));
			SysParam param = service.getSysParam(inData);
			if(!StringUtil.isBlank(dataType) && !dataType.equals(SysConst.ROOT_CODE)){
				inData.clear();
				inData.put("dataCode", dataType);
				inData.put("dataType", SysConst.ROOT_CODE);
				SysParam sysParam  = service.getSysParam(inData);
				dataTypeName = sysParam.getDataName();
			}
			this.putRenderInfo("dataTypeName", dataTypeName);
			this.putRenderInfo("param", param);
		} catch (Exception e) {
			throw new ActionException("查询系统参数数据错误"+e.getMessage(), e);
		}
		this.render("sys_param_form.html");
	}
	
	/**
	 * 保存参数
	 * @throws ActionException
	 */
	public void save() throws ActionException{
		boolean updateFlag = false;
		IData<String, Object> data = getData();
		String paramId = data.getString("paramId");
		String dataType = data.getString("dataType", SysConst.ROOT_CODE);
		SysParam param = new SysParam();
		if(!StringUtil.isBlank(paramId) && StringUtil.isNumber(paramId)){
			param.setId(Integer.parseInt(paramId));
			updateFlag = true;
		}
		param.setDataType(dataType);
		param.setDataName(data.getString("dataName"));
		param.setDataCode(data.getString("dataCode"));
		try {
			service.saveSysParam(param, updateFlag);
			this.showMsg("系统参数信息"+(updateFlag? "保存" : "新增")+"成功", "?method=query&dataType="+dataType);
		} catch (ComponentException e) {
			throw new ActionException("保存参数信息错误", e);
		}
	}
	
	public void delete() throws ActionException{
		IData<String, Object> data = getData();
		String dataId = data.getString("dataId", "");
		String dataType = data.getString("dataType", SysConst.ROOT_CODE);
		if(StringUtil.isBlank(dataId) || !StringUtil.isNumber(dataId)){
			this.alertMsg("参数传值错误");
			return;
		}
		try {
			int resultCode = service.deleteParam(Integer.parseInt(dataId));
			if(resultCode == 1){
				this.alertMsg("数据错误，请刷新后重试");
				return;
			}else if(resultCode == 2){
				this.alertMsg("参数分类下子参数信息，不能删除");
				return;
			}
			this.showMsg("系统参数信息删除成功", "?method=query&dataType="+dataType);
		} catch (ComponentException e) {
			throw new ActionException("删除参数信息错误", e);
		}
	}
	
	public void reload() throws ActionException{
		IData<String, Object> data = getData();
		String dataType = data.getString("dataType", SysConst.ROOT_CODE);
		try {
			StaticInfoQry.createCache();
			this.showMsg("刷新系统参数缓存信息成功", "?method=query&dataType="+dataType);
		} catch (Exception e) {
			throw new ActionException("保存参数信息错误", e);
		}
	}
}
